#!/usr/bin/env python
# encoding: utf-8
'''
TPlus -- shortdesc

TPlus is a description

It defines classes_and_methods

@author:     Anson Chan

@copyright:  2016 Xerland Co. Ltd. All rights reserved.

@contact:    anson_chan@xerland.com
@deffield    updated: Updated
'''
from Ui.MainFrame import Frame
import wx
from Adb.PowerMonitor import PowerMonitor
from Adb.RtdMonitor import RtdMonitor
from TpScheduler import TpScheduler
from Comm.MqHandler import MqHandler
import Queue
from Utils.SysConstants import SERVERAPI_CONFIGSERVICE
from Utils.TopicFormater import TopicFormater

if __name__ == '__main__':
    app = wx.App()

    upStreamQ = Queue.Queue()
    downStreamQ = Queue.Queue()
    mqHander = MqHandler()
    mqHander.setUpStreamQ(upStreamQ)
    mqHander.setDownStreamQ(downStreamQ)
    mqHander.start()
    mTpSched = TpScheduler(True)
    mTpSched.setUpStreamQ(upStreamQ)
    mTpSched.setDownStreamQ(downStreamQ)
    powerMonitor = PowerMonitor()
    rtdMonitor = RtdMonitor()
#    mTpSched.subTask(SERVERAPI_CONFIGSERVICE.getValue("mq_power_subscription_request"), powerMonitor.getMachPowerState)
#    mTpSched.subTask(SERVERAPI_CONFIGSERVICE.getValue("mq_rtd_subscription_request"), rtdMonitor.getHistory)
#    mTpSched.pubTask(6, SERVERAPI_CONFIGSERVICE.getValue("mq_power_subscription_update"), powerMonitor.getMachPowerState)
#    mTpSched.pubTask(6, SERVERAPI_CONFIGSERVICE.getValue("mq_rtd_subscription_update"), rtdMonitor.getUpdate)    

    mTpSched.subTask(TopicFormater.getSubSelfPeerX("/powerstate"), powerMonitor.getMachPowerState)#SERVERAPI_CONFIGSERVICE.getValue("mq_power_subscription_request")
    mTpSched.subTask(TopicFormater.getSubSelfPeerX("/rtd"), rtdMonitor.getHistory)   #SERVERAPI_CONFIGSERVICE.getValue("mq_rtd_subscription_request")
    mTpSched.subTask(TopicFormater.getSubSelfPeerX("/rtdpage"), rtdMonitor.getHistoryPage)
    powerQueryInterval = int(SERVERAPI_CONFIGSERVICE.getValue("tp_power_query_interval"))
    rtdQueryInterval = int(SERVERAPI_CONFIGSERVICE.getValue("tp_rtd_query_interval"))
    mTpSched.pubTask(powerQueryInterval, TopicFormater.getPubSelf("/powerstate/update"), powerMonitor.getMachPowerState)   #SERVERAPI_CONFIGSERVICE.getValue("mq_power_subscription_update")
    mTpSched.pubTask(rtdQueryInterval, TopicFormater.getPubSelf("/rtd/update"), rtdMonitor.getUpdate)    #SERVERAPI_CONFIGSERVICE.getValue("mq_rtd_subscription_update")

    mTpSched.start()    
    tPlusHandlerList = [mqHander,mTpSched]
    
    frame = Frame(exhandlerList = tPlusHandlerList)
#    frame.Centre()
#    frame.Show()
    app.MainLoop()